package com.leoman.controller.api;

import com.leoman.core.ErrorType;
import com.leoman.core.bean.Result;
import com.leoman.entity.Paper;
import com.leoman.exception.NoDataException;
import com.leoman.exception.ParamErrorException;
import com.leoman.service.PaperService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 纸条api
 * Created by 涂奕恒 on 2016/11/24 0018.
 */
@Controller
@RequestMapping(value = "api/paper")
public class PaperApi {

    @Autowired
    private PaperService paperService;

    /**
     * @api {post} /api/paper/list 1、纸条列表
     * @apiVersion 0.0.1
     * @apiName paper.list
     * @apiGroup paper
     *
     * @apiDescription 纸条列表
     *
     * @apiParam   {Integer}   userId                     用户Id
     * @apiParam   {Integer}   isMe                       是否是我发的，0=否，1=是，默认为0
     * @apiParam   {Integer}   pageNum                    页码
     * @apiParam   {Integer}   pageSize                   每页显示条数
     *
     * @apiSuccess {Object}    list
     * @apiSuccess {Integer}   list.id                    纸条ID
     * @apiSuccess {Integer}   list.paperId               信纸ID
     * @apiSuccess {Integer}   list.type                  类型，1=短叹，2=长吟
     * @apiSuccess {String}    list.content               内容
     * @apiSuccess {Integer}   list.isTop                 是否置顶，0=否，1=是
     * @apiSuccess {Integer}   list.praiseNum             点赞数
     * @apiSuccess {Integer}   list.commentNum            评论数
     * @apiSuccess {Integer}   list.collectNum            收藏数
     * @apiSuccess {Integer}   list.isPraise              是否点赞，0=否，1=是
     * @apiSuccess {Integer}   list.isCollect             是否收藏，0=否，1=是
     * @apiSuccess {Integer}   list.isAdmin               是否管理员发送，0=否，1=是
     * @apiSuccess {Integer}   list.userId                发送人ID
     * @apiSuccess {String}    list.regionInfo            发送人所在地名称
     * @apiSuccess {String}    list.nickName              发送人昵称
     * @apiSuccess {String}    list.avatar                发送人头像
     * @apiSuccess {String}    list.createTime            发送时间
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "page": {
     *              "totalNum": 0,
     *              "totalPage": 0,
     *              "currentPage": 1
     *          },
     *          "list": [
     *              {
     *                  "id" : 1,
     *                  "paperId" : 1,
     *                  "type" : 1,
     *                  "content" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草",
     *                  "isTop" : 1,
     *                  "praiseNum" : 124,
     *                  "commentNum" : 20,
     *                  "collectNum" : 20,
     *                  "isPraise" : 0,
     *                  "isCollect" : 0,
     *                  "isAdmin" : 0,
     *                  "userId" : 1,
     *                  "nickName" : "张三",
     *                  "regionInfo" : "",
     *                  "avatar" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "createTime" : "1476843673494"
     *              }
     *          ]
     *      }
     * }
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public void list(HttpServletResponse response, Integer userId, Integer isMe, Integer pageNum, Integer pageSize) {
        Page<Paper> page = paperService.iPageForApp(userId, isMe, pageNum, pageSize);
        Map<String, Object> dataMap = JsonUtil.fitting(page);
        String result = JsonUtil.obj2ApiJson(Result.successApi(dataMap), "user");
        WebUtil.printApi(response, result);
    }

    /**
     * @api {post} /api/paper/save 2、写纸条
     * @apiVersion 0.0.1
     * @apiName paper.save
     * @apiGroup paper
     *
     * @apiDescription 写纸条
     *
     * @apiParam   {Integer}   userId                     用户Id                    （必填）
     * @apiParam   {Integer}   tempId                     纸条草稿Id
     * @apiParam   {Integer}   paperId                    信纸Id                    （必填）
     * @apiParam   {Integer}   type                       类型，1=短叹，2=长吟       （必填）
     * @apiParam   {String}    content                    内容                      （必填）
     * @apiParam   {Integer}   isDraft                    是否草稿，0=否，1=是       （必填）
     *
     * @apiSuccess {Integer}   paperId                    纸条ID
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "paperId" : 1
     *      }
     * }
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(HttpServletResponse response, Integer userId, Integer tempId, Integer paperId, Integer type, String content, Integer isDraft) {
        try {
            Integer num = paperService.addPaperInfo(false, userId, tempId, paperId, type, content, isDraft);
            String result = JsonUtil.obj2ApiJson(Result.successInfoApi(num, "paperId"));
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, JsonUtil.obj2ApiJson(Result.failureApi(ErrorType.ERROR_NOT_PARAM)));
        }
    }

    /**
     * @api {post} /api/paper/draft 3、纸条草稿
     * @apiVersion 0.0.1
     * @apiName paper.draft
     * @apiGroup paper
     *
     * @apiDescription 纸条草稿
     *
     * @apiParam   {Integer}   userId                          用户Id               （必填）
     *
     * @apiSuccess {Object}    paperInfo
     * @apiSuccess {Integer}   paperInfo.id                    纸条草稿ID
     * @apiSuccess {Integer}   paperInfo.type                  类型，1=短叹，2=长吟
     * @apiSuccess {String}    paperInfo.content               内容
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "paperInfo": {
     *              "id" : 1,
     *              "type" : 1,
     *              "content" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草"
     *          }
     *      }
     * }
     */
    @RequestMapping(value = "/draft", method = RequestMethod.POST)
    public void draft(HttpServletResponse response, Integer userId) {
        try {
            Paper paper = paperService.iFindDraftByUserId(userId);
            String result = JsonUtil.obj2ApiJson(Result.successInfoApi(paper, "paperInfo"), "user", "paperId", "isTop", "praiseNum", "commentNum", "isPraise", "isCollect", "isAdmin", "userId", "nickName", "avatar", "createTIme");
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, JsonUtil.obj2ApiJson(Result.failureApi(ErrorType.ERROR_NOT_PARAM)));
        } catch (NoDataException e) {
            WebUtil.printApi(response, JsonUtil.obj2ApiJson(Result.failureApi(ErrorType.ERROR_NO_DATA)));
        }
    }
}
